package com.kgcx.order.service;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.order.domain.OrderDriver;
import com.kgcx.order.mapper.OrderDriverMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Service
public class OrderDriverService {

    private static Logger logger = LoggerFactory.getLogger(OrderDriverService.class);

    @Autowired
    private OrderDriverMapper odMapper;

    /**
     * 更新司机每日订单数
     *
     * @param driverId
     */
    public void add(Long driverId, Integer count, Integer amount, Timestamp time) {

        try {
            LocalDateTime localDateTime = time.toLocalDateTime();
            RequestData rd = new RequestData();
            rd.put("driver_id", driverId);
            rd.put("date_day", DateTimeUtils.convertNumberDay(localDateTime));
            OrderDriver od = odMapper.getTodayOrderDriver(rd);
            if (od == null) {
                OrderDriver odTmp = new OrderDriver();
                odTmp.setDateMonth(DateTimeUtils.convertNumberMonth(localDateTime));
                odTmp.setDateDay(DateTimeUtils.convertNumberDay(localDateTime));
                odTmp.setOrderCount(count);
                odTmp.setOrderAmount(amount);
                odTmp.setDriverId(driverId);
                odTmp.setStatus(Cons.COMMON_STATUS_ACTIVE);
                odMapper.insert(odTmp);
            } else {
                od.setOrderCount(od.getOrderCount() + count);
                od.setOrderAmount(od.getOrderAmount() + amount);
                odMapper.updateByPrimaryKey(od);
            }
        } catch (Exception e) {
            logger.error("增加每日订单数异常:driverid:" + driverId, e);
        }


    }
}
